package com.jackie.proximity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;


public class MainActivity extends Activity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    public static final String URL = "url";
    public static final String MUSIC = "music";

    private EditText etUrl;
    private TextView tvMusic;
    private String url;
    private String musicPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService(new Intent(this, SensorServer.class));
        etUrl = (EditText) findViewById(R.id.et_url);
        tvMusic = (TextView) findViewById(R.id.tv_music_path);
        tvMusic.setBackground(etUrl.getBackground());

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        url = preferences.getString(URL, "");
        musicPath = preferences.getString(MUSIC, "");
        etUrl.setText(url);
        tvMusic.setText(musicPath.substring(musicPath.lastIndexOf("/") + 1));

        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.btn_ok).setOnClickListener(this);
        tvMusic.setOnClickListener(this);
        etUrl.setOnClickListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btn_cancel:
                etUrl.setText("");
                tvMusic.setText("");
                break;
            case R.id.btn_ok:
                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit();
                url = etUrl.getText().toString();
                if(TextUtils.isEmpty(url) && TextUtils.isEmpty(musicPath)) {
                    Toast.makeText(this, "没有设置网址和音乐", Toast.LENGTH_LONG).show();
                } else {
                    if(TextUtils.isEmpty(url)) {
                        Toast.makeText(this, "没有设置网址", Toast.LENGTH_LONG).show();
                    } else {
                        editor.putString(URL, url).commit();
                    }
                    File musicFile = new File(musicPath);
                    if(!musicFile.exists()) {
                        Toast.makeText(this, "没有设置音乐", Toast.LENGTH_LONG).show();
                    } else {
                        editor.putString(MUSIC, musicPath).commit();
                    }

                    if(!TextUtils.isEmpty(url) && !TextUtils.isEmpty(musicPath)) {
                        Toast.makeText(this, "设置完成", Toast.LENGTH_LONG).show();
                        finish();
                    }
                }
                break;
            case R.id.tv_music_path:
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                this.startActivityForResult(intent, R.id.tv_music_path);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();
            Log.i(TAG, uri.toString());
            String path = uri.getPath();
            musicPath = path;
            Log.i(TAG, path);
            switch(requestCode) {
                case R.id.tv_music_path:
                    if(("mp3").equals(path.substring(path.lastIndexOf(".") + 1)) && new File(path).exists()) {
                        tvMusic.setText(path.substring(path.lastIndexOf("/") + 1));
                    } else {
                        Toast.makeText(this, "请选择mp3格式的文件", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    }
}
